using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Collections;

namespace LdExpress
{
    public class Tasking
    {
        public long ID = -1;
        public string Name = "";
        public string Summary = "";
        public string Detail = "";
        public string Comment = "";
        public DateTime CreatedDate = DateTime.Now;
        public long CreatedByID = -1;
        public DateTime LastUpdatedDate = DateTime.Now;
        public long LastUpdatedByID = -1;
        public DateTime StartDate = DateTime.Now;
        public DateTime EndDate = DateTime.Now;
        public int IsCompleted = 0;
        public long OwnerID = -1;
        public long SourceID = -1;
        public string SourceName = "";
        public int Sort = 0;
        public string FileName1 = "";
        public string FileName2 = "";
        public int priority = 0;
        public Tasking()
        {
        }
        public Tasking(long lID)
        {
            ID = lID;
        }

        #region Basic methods
        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            if (ID <= 0) // new object, we insert new record to database
            {
                // build SQL statement
                strSQL = "Insert into tbl_tasking (Name,Summary,Detail,Comment" +
                    ",CreatedDate,CreatedByID,LastUpdatedDate,LastUpdatedByID," +
                    "StartDate,EndDate,IsCompleted,OwnerID,SourceID,SourceName,Sort,FileName1,FileName2, priority) " +
                    "values (N'" + DBModule.RefineString(Name) + "',N'" +
                    DBModule.RefineString(Summary) + "',N'" + DBModule.RefineString(Detail) + "',N'" + DBModule.RefineString(Comment) + "'," +
                    DBModule.RefineDatetime(CreatedDate, true) + "," + CreatedByID + "," +
                    DBModule.RefineDatetime(LastUpdatedDate, true) + "," + LastUpdatedByID + "," +
                    DBModule.RefineDatetime(StartDate, true) + "," + DBModule.RefineDatetime(EndDate, true) + "," + IsCompleted.ToString() + "," +
                    OwnerID.ToString() + "," + SourceID.ToString() + ",N'" + DBModule.RefineString(SourceName) + "'," + Sort.ToString()
                    + ",N'" + DBModule.RefineString(FileName1) + "'" + ",N'" + DBModule.RefineString(FileName2) + "'," + priority.ToString() + ")";
                UserObjectsLog.SaveObjectActionLog("Tasking", "add", cn, trans);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update tbl_tasking set Name = N'" + DBModule.RefineString(Name) + "'," +
                    "Summary=N'" + DBModule.RefineString(Summary) + "'," +
                    "Detail=N'" + DBModule.RefineString(Detail) + "'," +
                    "Comment=N'" + DBModule.RefineString(Comment) + "'," +
                    //"CreatedByID=" + CreatedByID + "," +
                    "CreatedDate=" + DBModule.RefineDatetime(CreatedDate, true) + "," +
                    "LastUpdatedDate=" + DBModule.RefineDatetime(LastUpdatedDate, true) + "," +
                    "LastUpdatedByID=" + LastUpdatedByID + "," +
                    "StartDate=" + DBModule.RefineDatetime(StartDate, true) + "," +
                    "EndDate=" + DBModule.RefineDatetime(EndDate, true) + "," +
                    "IsCompleted=" + IsCompleted.ToString() + "," +
                    "OwnerID=" + OwnerID.ToString() + ", " +
                    "SourceID=" + SourceID.ToString() + ", " +
                    "SourceName=N'" + DBModule.RefineString(SourceName) + "' " +
                    ",Sort=" + Sort.ToString() +
                    ",FileName1=N'" + DBModule.RefineString(FileName1) + "' " +
                    ",FileName2=N'" + DBModule.RefineString(FileName2) + "' " +
                    ",priority=" + priority.ToString() +
                    " where ID=" + ID.ToString();
                UserObjectsLog.SaveObjectActionLog("Tasking", "edit", cn, trans);
            }
            // run SQL statement
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
        }
        public virtual void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "Delete from tbl_tasking where ID=" + ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("Tasking", "delete", cn, trans);
        }

        public void Load(long lID, MySqlConnection cn, MySqlTransaction trans)
        {
            // Error retError = null;
            string strSQL = "Select * from tbl_tasking where ID=" + ID.ToString();
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);

            // fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("Name")) Name = dr["Name"].ToString();
                if (!dr.IsNull("Summary")) Summary = dr["Summary"].ToString();
                if (!dr.IsNull("Detail")) Detail = dr["Detail"].ToString();
                if (!dr.IsNull("Comment")) Comment = dr["Comment"].ToString();
                if (!dr.IsNull("CreatedDate")) CreatedDate = DateTime.Parse(dr["CreatedDate"].ToString());
                if (!dr.IsNull("CreatedByID")) CreatedByID = long.Parse(dr["CreatedByID"].ToString());
                if (!dr.IsNull("LastUpdatedDate")) CreatedDate = DateTime.Parse(dr["LastUpdatedDate"].ToString());
                if (!dr.IsNull("LastUpdatedByID")) CreatedByID = long.Parse(dr["LastUpdatedByID"].ToString());
                if (!dr.IsNull("StartDate")) StartDate = DateTime.Parse(dr["StartDate"].ToString());
                if (!dr.IsNull("EndDate")) EndDate = DateTime.Parse(dr["EndDate"].ToString());
                if (!dr.IsNull("IsCompleted")) IsCompleted = int.Parse(dr["IsCompleted"].ToString());
                if (!dr.IsNull("OwnerID")) OwnerID = long.Parse(dr["OwnerID"].ToString());
                if (!dr.IsNull("SourceID")) SourceID = long.Parse(dr["SourceID"].ToString());
                if (!dr.IsNull("SourceName")) SourceName = dr["SourceName"].ToString();
                if (!dr.IsNull("Sort")) Sort = int.Parse(dr["Sort"].ToString());
                if (!dr.IsNull("FileName1")) FileName1 = dr["FileName1"].ToString();
                if (!dr.IsNull("FileName2")) FileName2 = dr["FileName2"].ToString();
                if (!dr.IsNull("priority")) priority = int.Parse(dr["priority"].ToString());
            }

        }
        public void GetList(string strIDs, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_tasking where 1=1 ";
            if ((strIDs != "") && (strIDs != null))
            {
                if (strIDs != "All")
                    strSQL += strIDs;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            //GV.Response.Write("sql=" + strSQL);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        #endregion
        public static void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_tasking where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans); ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}
